package cn.iocoder.yudao.module.biz.controller.admin.staffDuty;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.staffDuty.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.staffDuty.BizStaffDutyDO;
import cn.iocoder.yudao.module.biz.convert.staffDuty.BizStaffDutyConvert;
import cn.iocoder.yudao.module.biz.service.staffDuty.BizStaffDutyService;

@Api(tags = "管理后台 - 职责与考核标准")
@RestController
@RequestMapping("/biz/staff-duty")
@Validated
public class BizStaffDutyController {

    @Resource
    private BizStaffDutyService staffDutyService;

    @PostMapping("/create")
    @ApiOperation("创建职责与考核标准")
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:create')")
    public CommonResult<String> createStaffDuty(@Valid @RequestBody BizStaffDutyCreateReqVO createReqVO) {
        return success(staffDutyService.createStaffDuty(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新职责与考核标准")
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:update')")
    public CommonResult<Boolean> updateStaffDuty(@Valid @RequestBody BizStaffDutyUpdateReqVO updateReqVO) {
        staffDutyService.updateStaffDuty(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除职责与考核标准")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:delete')")
    public CommonResult<Boolean> deleteStaffDuty(@RequestParam("id") String id) {
        staffDutyService.deleteStaffDuty(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得职责与考核标准")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:query')")
    public CommonResult<BizStaffDutyRespVO> getStaffDuty(@RequestParam("id") String id) {
        BizStaffDutyDO staffDuty = staffDutyService.getStaffDuty(id);
        return success(BizStaffDutyConvert.INSTANCE.convert(staffDuty));
    }

    @GetMapping("/list")
    @ApiOperation("获得职责与考核标准列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:query')")
    public CommonResult<List<BizStaffDutyRespVO>> getStaffDutyList(@RequestParam("ids") Collection<String> ids) {
        List<BizStaffDutyDO> list = staffDutyService.getStaffDutyList(ids);
        return success(BizStaffDutyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得职责与考核标准分页")
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:query')")
    public CommonResult<PageResult<BizStaffDutyRespVO>> getStaffDutyPage(@Valid BizStaffDutyPageReqVO pageVO) {
        PageResult<BizStaffDutyDO> pageResult = staffDutyService.getStaffDutyPage(pageVO);
        return success(BizStaffDutyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出职责与考核标准 Excel")
    @PreAuthorize("@ss.hasPermission('biz:staff-duty:export')")
    @OperateLog(type = EXPORT)
    public void exportStaffDutyExcel(@Valid BizStaffDutyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizStaffDutyDO> list = staffDutyService.getStaffDutyList(exportReqVO);
        // 导出 Excel
        List<BizStaffDutyExcelVO> datas = BizStaffDutyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "职责与考核标准.xls", "数据", BizStaffDutyExcelVO.class, datas);
    }

}
